package com.ktjiaoyu.server.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktjiaoyu.server.mapper.AdminMapper;
import com.ktjiaoyu.server.domain.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {


    @Autowired
    private AdminMapper adminMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);

        Admin sysUser = adminMapper.selectOne(queryWrapper);

        if (sysUser==null){
            throw new RuntimeException("用户不存在");
        }

        // 权限信息 todo 要从数据库里查
        List<String> perms = new ArrayList<>();// sysUserMapper.findPermsByUserId(sysUser.getId());

        LoginUser loginUser = new LoginUser(sysUser,perms);

        return loginUser;
    }
}
